package com.ruoyi.develop.produceRecord.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.lvDaohuo.domain.LvDaohuo;
import com.ruoyi.develop.lvDaohuoDetail.domain.LvDaohuoDetail;
import com.ruoyi.develop.lvRuku.service.ILvRukuService;
import com.ruoyi.develop.produceRecordDetail.domain.ProduceRecordDetail;
import com.ruoyi.develop.produceRecordDetail.service.IProduceRecordDetailService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.produceRecord.domain.ProduceRecord;
import com.ruoyi.develop.produceRecord.service.IProduceRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.develop.lvRukumingxi.utils.RukumingxiUtils;

/**
 * 生产记录Controller
 * @author lmm
 * @date 2025-05-16
 */
@RestController
@RequestMapping("/produceRecord/produceRecord")
public class ProduceRecordController extends BaseController {
    @Autowired
    private IProduceRecordService produceRecordService;
    @Autowired
    private IProduceRecordDetailService produceRecordDetailService;
    @Autowired
    private ILvRukuService lvRukuService;





    /**
     * 查询生产记录列表
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecord:query')")
    @GetMapping("/list")
    public TableDataInfo list(ProduceRecord entity) {
        startPage();
        List<ProduceRecord> list = produceRecordService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出生产记录列表
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecord:export')")
    @Log(title = "生产记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProduceRecord entity) {
        List<ProduceRecord> list = produceRecordService.selectList(entity);
        ExcelUtil<ProduceRecord> util = new ExcelUtil<ProduceRecord>(ProduceRecord. class);
        util.exportExcel(response, list, "生产记录数据");
    }

    /**
     * 获取生产记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(produceRecordService.selectById(id));
    }

    /**
     * 新增生产记录
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecord:add')")
    @Log(title = "生产记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProduceRecord entity) {
        return toAjax(produceRecordService.insert(entity));
    }

    /**
     * 修改生产记录
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecord:edit')")
    @Log(title = "生产记录", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ProduceRecord entity) {
        return toAjax(produceRecordService.update(entity));
    }

    /**
     * 删除生产记录
     */
    @PreAuthorize("@ss.hasPermi('develop:ProduceRecord:remove')")
    @Log(title = "生产记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(produceRecordService.deleteByIds(ids));
    }

    /** * 提交质检 */
    @GetMapping(value = "/submit/{id}")
    public AjaxResult submit(@PathVariable("id") String id) {
        ProduceRecord produceRecord = produceRecordService.selectById(id);
        if(BusinessConstants.PRODUCE_RECORD_STATUS_SUBMIT.equals(produceRecord.getStatus())){
            return AjaxResult.error("该生产单已经提交！");
        }
        produceRecord.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_SUBMIT);
        produceRecordService.update(produceRecord);
        ProduceRecordDetail detail = new ProduceRecordDetail();
        detail.setProduceId(id);
        List<ProduceRecordDetail> list = produceRecordDetailService.selectList(detail);
        for (ProduceRecordDetail recordDetail : list) {
            recordDetail.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_SUBMIT);
            produceRecordDetailService.update(recordDetail);
        }
        return AjaxResult.success("提交成功");
    }



    /** * 质检通过 */
    @GetMapping(value = "/inspectPass/{id}")
    public AjaxResult inspectPass(@PathVariable("id") String id) {
        ProduceRecord produceRecord = produceRecordService.selectById(id);
        if(BusinessConstants.PRODUCE_RECORD_STATUS_INSPECT_PASS.equals(produceRecord.getStatus())){
            return AjaxResult.error("该生产单已经审核通过！");
        }
        produceRecord.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_INSPECT_PASS);
        produceRecordService.update(produceRecord);
        ProduceRecordDetail detail = new ProduceRecordDetail();
        detail.setProduceId(id);
        List<ProduceRecordDetail> list = produceRecordDetailService.selectList(detail);
        for (ProduceRecordDetail recordDetail : list) {
            recordDetail.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_INSPECT_PASS);
            produceRecordDetailService.update(recordDetail);
        }

        return AjaxResult.success("质检通过");
    }

    /** * 质检不通过 */
    @GetMapping(value = "/inspectReject/{id}")
    public AjaxResult inspectReject(@PathVariable("id") String id) {
        ProduceRecord produceRecord = produceRecordService.selectById(id);
        if(BusinessConstants.PRODUCE_RECORD_STATUS_INSPECT_REJECT.equals(produceRecord.getStatus())){
            return AjaxResult.error("该生产单已经审核通过！");
        }
        produceRecord.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_INSPECT_REJECT);
        produceRecordService.update(produceRecord);
        ProduceRecordDetail detail = new ProduceRecordDetail();
        detail.setProduceId(id);
        List<ProduceRecordDetail> list = produceRecordDetailService.selectList(detail);
        for (ProduceRecordDetail recordDetail : list) {
            recordDetail.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_INSPECT_REJECT);
            produceRecordDetailService.update(recordDetail);
        }

        return AjaxResult.success("质检退回成功");
    }


    /** * 库管通过 */
    @GetMapping(value = "/storePass/{id}")
    public AjaxResult storePass(@PathVariable("id") String id) {
        ProduceRecord produceRecord = produceRecordService.selectById(id);
        if(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_PASS.equals(produceRecord.getStatus())){
            return AjaxResult.error("该生产单已经审核通过！");
        }
        produceRecord.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_PASS);
        produceRecordService.update(produceRecord);
        ProduceRecordDetail detail = new ProduceRecordDetail();
        detail.setProduceId(id);
        List<ProduceRecordDetail> list = produceRecordDetailService.selectList(detail);
        for (ProduceRecordDetail recordDetail : list) {
            recordDetail.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_PASS);
            produceRecordDetailService.update(recordDetail);
        }
        // 库管通过审核 入库
        lvRukuService.rukuByProduce(produceRecord,list);
        return AjaxResult.success("审核通过");
    }


    /** * 库管退回 */
    @PutMapping(value = "/storeReject")
    public AjaxResult storeReject(@RequestBody ProduceRecord produceRecord) {
        if(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_REJECT.equals(produceRecord.getStatus())){
            return AjaxResult.error("该生产单已经审核通过！");
        }
        produceRecord.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_REJECT);
        produceRecordService.update(produceRecord);
        ProduceRecordDetail detail = new ProduceRecordDetail();
        detail.setProduceId(produceRecord.getId());
        List<ProduceRecordDetail> list = produceRecordDetailService.selectList(detail);
        for (ProduceRecordDetail recordDetail : list) {
            recordDetail.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_REJECT);
            produceRecordDetailService.update(recordDetail);
        }
        return AjaxResult.success("库管退回成功");
    }


    /** * 库管入库 退回 */
    @PutMapping(value = "/storeReturn")
    public AjaxResult storeReturn(@RequestBody ProduceRecord produceRecord) {
        if(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_REJECT.equals(produceRecord.getStatus())){
            return AjaxResult.error("该生产单已经退回！");
        }
        produceRecord.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_REJECT);
        produceRecordService.update(produceRecord);
        ProduceRecordDetail detail = new ProduceRecordDetail();
        detail.setProduceId(produceRecord.getId());
        List<ProduceRecordDetail> list = produceRecordDetailService.selectList(detail);
        for (ProduceRecordDetail recordDetail : list) {
            recordDetail.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_STORE_REJECT);
            produceRecordDetailService.update(recordDetail);
        }
        // 库管通过审核 入库
        lvRukuService.chukuByProduce(produceRecord,list);
        return AjaxResult.success("审核通过");
    }







}
